package com.yxw.yxnet_cd_center.customer_service.service.impl;

import com.yxw.yxnet_cd_center.common.enums.CreateTableEnum;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsNoticeSendEntity;
import com.yxw.yxnet_cd_center.customer_service.mapper.LogMapper;
import com.yxw.yxnet_cd_center.customer_service.service.CreateTableService;
import com.yxw.yxnet_cd_center.customer_service.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 创建表service
 * @author liuxin
 * @date 2023.09.25
 */
@Slf4j
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private CreateTableService  createTableService;

    @Autowired
    private LogMapper  logMapper;

    /**
     * 推送记录保存
     * @param entityList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveNoticeSentLog(List<YxCsNoticeSendEntity> entityList) {
        if(null!=entityList && !entityList.isEmpty()){
            try{
                //创建记录表
                String tableName = createTableService.createTable(CreateTableEnum.YX_CS_NOTICE_SEND);
                //每次保存条数
                Integer sizePage=1000;
                //总条数
                Integer totalSize = entityList.size();
                //截取次数
                Integer count=totalSize%sizePage==0?totalSize/sizePage:totalSize/sizePage+1;
                for (int i = 0; i < count; i++) {
                    //新增记录
                    logMapper.saveNoticeSentLog(entityList.subList(i * sizePage, i == count - 1 ? totalSize : (1 + i) * sizePage), tableName);
                }
            }catch (Exception e){
                log.error("写入记录出错",e);
            }
        }
    }
}
